package com.andculture.puremvcflexunittesting.example.model {
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;

	public class UserProxy extends Proxy implements IProxy {
		
		/**
		 * Proxy name
		 */
		public static const NAME:String = "UserProxy";
		
		/****************************************
		 * NOTIFICATIONS
		****************************************/
		
		// List users
		public static const LIST_USERS:String = "listUsers";
		public static const LIST_USERS_RESPONSE:String = "listUsersResponse";
		
		/****************************************
		 * PRIVATE VARIABLES
		****************************************/
		
		private var _users:Array = [ "Larry", "Fran", "andCulture" ];
		
		/**
		 * Constructor.
		 */
		public function UserProxy() {
			super(NAME);
		}
		
		/****************************************
		 * PUBLIC METHODS
		****************************************/
		
		/**
		 * Retrieve a list of users asynchronously.
		 */
		public function listUsers():void {
			// Since this is just an example, fake a web service call
			// with a Timer.
			var timer:Timer = new Timer(500, 1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, handleListUsersResponse);
			timer.start();
		}
		
		/****************************************
		 * EVENT HANDLERS
		****************************************/
		
		/**
		 * Event handler listening for listUsers() result.
		 * 
		 * @ param e Event object describing the event.
		 */
		private function handleListUsersResponse(e:Event):void {
			// Fake web service request is complete. Send out the list
			// of users.
			sendNotification(LIST_USERS_RESPONSE, _users);
		}
	}
}